package ThreadDemo;

/*
 * Author: JacksonCoder
 * FileName: CompletableFutureExceptionHandling
 * Version: 1.0.0
 * Date: 2025/2/21
 */

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureExceptionHandling {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture.supplyAsync(() -> {
            if (true) {
                throw new RuntimeException("Something went wrong!");
            }
            return "hello";
        }).exceptionally(ex -> {
            System.out.println("Exception: " + ex.getMessage());
            return "Recovered from exception";
        }).thenAcceptAsync(result -> {
            System.out.println(result); //   输出： "Recovered from exception"
        }).get();
    }

}
